package com.qili.service.supervisor.impl;

import com.qili.base.entity.BasePathConfig;
import com.qili.base.mapper.BaseMapper;
import com.qili.base.service.impl.BaseServiceImpl;
import com.qili.entity.supervisor.SupervisorFile;
import com.qili.entity.supervisor.SupervisorFileTemp;
import com.qili.entity.supplier.SupplierProject;
import com.qili.entity.sys.SysLog;
import com.qili.mapper.supervisor.SupervisorFileTempMapper;
import com.qili.mapper.sys.SysLogMapper;
import com.qili.service.supervisor.SupervisorFileService;
import com.qili.service.supervisor.SupervisorFileTempService;
import com.qili.util.*;
import lombok.extern.java.Log;
import org.apache.commons.io.FileUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Log
@Service
public class SupervisorFileTempServiceImpl extends BaseServiceImpl<SupervisorFileTemp,String> implements SupervisorFileTempService {

    @Autowired
    private SupervisorFileTempMapper supervisorFileTempMapper;
    @Autowired
    UploadUtil uploadUtil;

    @Autowired
    SupervisorFileService  supervisorFileService;

    @Override
    public BaseMapper<SupervisorFileTemp, String> getMappser() {
        return supervisorFileTempMapper;
    }


    @Autowired
    SysLogMapper sysLogMapper;

    // 获取存储文件夹
    @Override
    public String   getCopyZskUrl(SupplierProject supProject)  throws  Exception{
        String zskskUrl = supProject.getZskurl();
        String proName="";
        if(StringUtil.isBlank(zskskUrl)){
            proName=supProject.getProjectName()+supProject.getOrderNum();
            String ptypename="";
            String basePathId="";
            if(StringUtil.isNotBlank(supProject.getProjectType())){
                if(supProject.getProjectType().indexOf("001")>-1){
                    ptypename="监理";
                    basePathId="99001";
                }else if(supProject.getProjectType().indexOf("002")>-1){
                    ptypename="咨询";
                    basePathId="99002";
                }else if(supProject.getProjectType().indexOf("003")>-1){
                    ptypename="检测";
                    basePathId="99003";
                }else if(supProject.getProjectType().indexOf("100")>-1){
                    ptypename="其他";
                    basePathId="99004";
                }
            }
            // 需要将文件移动到 综合库/供需服务系统/监理/**项目
            UploadType uploadType = getFileUploadType(proName,basePathId);
            String  AsSubPath = FilePathUtil.getSavePathByFileType(uploadType);
            //文件主路径
            String AsMainPath =uploadUtil.getUploadPath();
            String AsPath = AsMainPath + AsSubPath;
            zskskUrl = FilePathUtil.doFileSeparator(AsPath);
        }
        return  zskskUrl;

    }


    @Override
    public void   updateCopyFileToZsk(String AsPath,SupplierProject  supplierProject , List<SupervisorFileTemp>  filelist)  throws  Exception{
        SupervisorFileTemp temp=new SupervisorFileTemp();
        temp.setLinkId(supplierProject.getSupProjectId());
        log.info("进入1111000000000000000000000000888888888888888888888");
        for (int i = 0; i < filelist.size(); i++) {
            SupervisorFileTemp supervisorFileTemp = filelist.get(i);
            String type = supervisorFileTemp.getType();
            //将招标文件、投标文件、放到IT服务知识库中技术方案库中，合同放到IT服务知识库中的产品信息库中
            //招投标说明文件、合同说明文件放到综合库中
            //招标文件说明文件

            //上传项目招标文件说明文件    311
            //上传项目招标文件312
            //投标文件 313
            //上传合同 314
            //合同说明文件  315
            // 301 应接人 上传的投标文件
            //  上传中标通知书   316
            if("311".equals(type)  ||"315".equals(type)    ||"316".equals(type) ){


            }else  if("313".equals(type)  ||  "301".equals(type)  ){
                // 2 2 技术方案库\投标文件
                log.info("进入teststseeteTEDT0000000count0088898909090900投标文件");
                UploadType uploadType = getFileUploadType("投标文件","2");
                String  AsSubPath = FilePathUtil.getSavePathByFileType(uploadType);
                //文件主路径
                String AsMainPath =uploadUtil.getUploadPath();
                log.info("进入teststseeteTEDT0000000count0088898909090900后后投标文件");
                AsPath = FilePathUtil.doFileSeparator(AsMainPath + AsSubPath);
                log.info("进入teststseeteTEDT0000000count0088898909090900后后投标文件11111111");
            }else if("312".equals(type)){
                log.info("进入teststseeteTEDT0000000count0088898909090900招标文件");
                UploadType uploadType = getFileUploadType("招标文件","1");
                String  AsSubPath = FilePathUtil.getSavePathByFileType(uploadType);
                //文件主路径
                String AsMainPath =uploadUtil.getUploadPath();
                AsPath = FilePathUtil.doFileSeparator(AsMainPath + AsSubPath);

            }else if("314".equals(type)){
                log.info("进入teststseeteTEDT0000000count0088898909090900招标文件");
                // 产品信息库
                UploadType uploadType = getFileUploadType("招标文件","9");
                String  AsSubPath = FilePathUtil.getSavePathByFileType(uploadType);
                //文件主路径
                String AsMainPath =uploadUtil.getUploadPath();
                AsPath = FilePathUtil.doFileSeparator(AsMainPath + AsSubPath);

            }
            File AfTmp = new File(AsPath);
            if (!AfTmp.exists()) {
                AfTmp.mkdirs();
            }
            String temfilename=supervisorFileTemp.getFileName();
            String substring ="";
            if("301".equals(type) || "312".equals(type)  || "313".equals(type)  || "314".equals(type)  ){
                substring = temfilename.substring(0, temfilename.lastIndexOf("."))+"_"+supplierProject.getProjectName()+"_"+supplierProject.getSupProjectId()+supervisorFileTemp.getFileType();
            }else{
                substring = temfilename.substring(0, temfilename.lastIndexOf("."))+"_"+supplierProject.getSupProjectId()+supervisorFileTemp.getFileType();
            }
            log.info("进入teststseete0招标文件");
            LoadType loadType = new LoadType();
            loadType.setCurrentFile(new File(AsPath + "/" + substring));
            String url = supervisorFileTemp.getUrl();
            File currentFile = loadType.getCurrentFile();
            String path = currentFile.getPath();
            if(!url.equals(path)){
                FileUtils.copyFile(new File(supervisorFileTemp.getUrl()),loadType.getCurrentFile());
            }
            // 复制文件结束---------------------------------
            // 插入正式表开始--------------------------------
            SupervisorFile filecount = new SupervisorFile();
            filecount.setProjectId(supplierProject.getSupProjectId());
            filecount.setType(supervisorFileTemp.getType());
            filecount.setUrl(loadType.getFilePath());
            int count = supervisorFileService.selectCount(filecount);
            log.info("进入1111000000000000count000000000000000000");
            if(count==0){
                SupervisorFile filedto = new SupervisorFile();
                filedto.setFileName(supervisorFileTemp.getFileName());
                filedto.setFileType(supervisorFileTemp.getFileType());
                filedto.setType(supervisorFileTemp.getType());
                filedto.setCreateBy(supervisorFileTemp.getCreateBy());
                filedto.setCreateDate(new Date());
                filedto.setUrl(loadType.getFilePath());
                // 临时文件Id
                filedto.setLinkId(supervisorFileTemp.getId());
                // filedto.setInd();
                //filedto.setParentId(dto.getParentId());
                // filedto.setPositionName(dto.getPositionName());
                filedto.setProjectId(supplierProject.getSupProjectId());// 项目id\
                log.info("进入1111插入文件0000");
                supervisorFileService.insert(filedto);
                supervisorFileService.setFilePathDics(filedto);
                // 更新 临时文件的路径
                supervisorFileTemp.setUrl(loadType.getFilePath());
                log.info("进入777777777入文件008888888888888888888888800");
                supervisorFileTempMapper.updateByPrimaryKey(supervisorFileTemp);
                log.info("进入44444插入文件008888888888888888888888800");
            }
        }
    }


    private UploadType getFileUploadType( String projectName,String basePathId ) throws Exception {
        UploadType uploadType = prepareFileUploadType( projectName,basePathId);
        return uploadType;
    }
    private UploadType prepareFileUploadType(String projectName,String basePathId) {
        return prepareUploadType(0, projectName, basePathId);
    }

    public UploadType prepareUploadType(int type,String projectName ,String basePathId) {
        // 替换项目名称
        ArrayList<String> placeHolder = new ArrayList<String>();
        placeHolder.add(projectName);
        UploadType uploadType = UploadType.getEnumByType(type);
        //综合库\供需服务系统\监理\**项目
        //综合库\供需服务系统\咨询\**项目
        uploadType.setFilePathId(basePathId);
        uploadType.setPlaceholder(placeHolder);
        return uploadType;
    }

   @Override
    public   void  insertTolalLog(String  type,String  text,String projectId){
        SupervisorFileTemp supervisorFileTemp = new SupervisorFileTemp();
        supervisorFileTemp.setLinkId(projectId);
        List<SupervisorFileTemp> select = supervisorFileTempMapper.select(supervisorFileTemp);
        SysLog sysLog = new SysLog();
        sysLog.setCreateTime(new Date());
        sysLog.setType(type);
        sysLog.setText(text);
        sysLog.setParam("文件数量===="+select.size()+"文件详情"+select.toString());
        sysLog.setIp(projectId);
        sysLog.setCreateTime(DateUtil.parseDate(DateUtil.getCurrentDateString()));
        sysLog.setUserName(CommonUtil.getUser().getUsername());
        sysLogMapper.insert(sysLog);
    }




}
